package cn.hdu.liang;

import java.io.IOException;
import java.io.InputStream;

public class Request {
    private InputStream input;
    private String uri;

    public Request(InputStream input) {
        this.input = input;
    }

    public void parse() {
        StringBuffer req = new StringBuffer(2048);
        int i;
        byte[] buffer = new byte[2048];
        try {
            i = input.read(buffer);
        } catch(IOException e) {
            e.printStackTrace();
            i = -1;
        }
        for(int j = 0; j < i; j++) {
            req.append((char) buffer[j]);
        }
        System.out.print(req.toString());
        uri = parseUri(req.toString());
    }

    private String parseUri(String reqString) {
        int index1;
        int index2;
        index1 = reqString.indexOf(' ');
        if(index1 != -1) {
            index2 = reqString.indexOf(' ', index1 + 1);
            if(index2 > index1) {
                return reqString.substring(index1 + 1, index2);
            }
        }
        return null;
    }

    public String getUri() {
        return uri;
    }
}
